package com.shop.iterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.shop.domain.CustomSeller;


public class LoginIterceptor implements HandlerInterceptor{

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		System.out.println("===LoginInterceptor.preHandle(HttpServletRequest request="+request+", HttpServletResponse response="+response+", Object handler="+handler+")===");
		//1.判断访问的地址是否是公开地址，如果是则放行
		
		//2.判断用户是否成功登录，如果成功登录则放行，否则拦截
		HttpSession session=request.getSession();
		CustomSeller customSeller=(CustomSeller) session.getAttribute("customSeller");
	    System.out.println("customSeller="+customSeller);
		//说明用户成功登录过
		if(customSeller!=null){
			return true;
		}
		
		session.setAttribute("message", "您还没有登录，请先登录！");
		String contextPath=request.getContextPath();
		System.out.println("contextPath="+contextPath);
		response.sendRedirect(contextPath+"/seller/public/shoplogin.jsp");
		return false;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

}
